/*Class used to insert values into data base
 *
 */

package anton.dinner;

import java.io.Serializable;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import javax.annotation.Resource;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.sql.DataSource;

/**
 *
 * @author johan
 */

@ManagedBean
@SessionScoped

public class CreateDinner implements Serializable{
    
    @Resource(name="jdbc/antonsDatasource")
    
     private DataSource ds;
    
    private String dinnerName;
    private String dinnerDescription;
    private int dinnerPrice;
    private String dinnerType;
    int i;
    int itemId;
   
    
/*-----------------------------------------------------------------------------*/
/*setters and getters*/    
    public int getItemId() {
        return itemId;
    }

    public void setItemId(int itemId) {
        this.itemId = itemId;
    }
   
    /**
     * @return the dinnerName
     */
    public String getDinnerName() {
        return dinnerName;
    }

    /**
     * @param dinnerName the dinnerName to set
     */
    public void setDinnerName(String dinnerName) {
        this.dinnerName = dinnerName;
    }

    /**
     * @return the dinnerDescription
     */
    public String getDinnerDescription() {
        return dinnerDescription;
    }

    /**
     * @param dinnerDescription the dinnerDescription to set
     */
    public void setDinnerDescription(String dinnerDescription) {
        this.dinnerDescription = dinnerDescription;
    }

    /**
     * @return the dinnerPrice
     */
    public int getDinnerPrice() {
        return dinnerPrice;
    }

    /**
     * @param dinnerPrice the dinnerPrice to set
     */
    public void setDinnerPrice(int dinnerPrice) {
        this.dinnerPrice = dinnerPrice;
    }

    /**
     * @return the dinnerType
     */
    public String getDinnerType() {
        return dinnerType;
    }

    /**
     * @param dinnerType the dinnerType to set
     */
    public void setDinnerType(String dinnerType) {
        this.dinnerType = dinnerType;
    }
    
/*----------------------------------------------------------------------------*/
// method used to  insert into database tables Dinner and DinnerIngredients
    
    public  String add( ) throws SQLException {
       
        //sql query insert values into database
       String sql= "insert into Dinner(dinner_name,dinner_description,dinner_price,dinner_type ) values (?,?,?,?)";
       String sql2="insert into DinnerIngredients (dinner_name,item_ID ) values (?,?)";
      
       if(ds==null)
			throw new SQLException("Can't get data source");// throws exception i database not available
        try (Connection con = ds.getConnection()) {
            if(con==null)
                throw new SQLException("Can't get database connection");
            
            try (PreparedStatement ps = con.prepareStatement(sql )){
                
                ps.setString( 1, dinnerName );
                ps.setString(2, dinnerDescription);
                ps.setInt(3, dinnerPrice);
                ps.setString(4, dinnerType);
                
                // performs insertion
               i= ps.executeUpdate();             
            }
            
            if (itemId!=0){
                
                if(ds==null)
                    throw new SQLException("Can't get data source");// throws exception if datasource not available     
                
                //database connection
                if(con==null)
                    throw new SQLException("Can't get database connection");
                try (PreparedStatement ps = con.prepareStatement(
                       sql2)) {
                    
                    ps.setString( 1, dinnerName );
                    
                    ps.setInt(2, itemId);
                    
                    i= ps.executeUpdate();
                    
                    con.close();
                    reset();
                }}
        }
         
       
        
        
         return "createdinner"; 
     
        
    } // ends method add 
    
    
    public void reset()
    {
    this.dinnerName=null;
    this.dinnerDescription=null;
    this.dinnerPrice=0;
    this.itemId=0;
    this.dinnerType=null;
   
    
    }
    
}
